import 'package:flutter/material.dart';
import 'package:hi_base/view_util.dart';

class HFlexibleHeader extends StatefulWidget {
  final String name;
  final String face;
  final ScrollController controller;
  HFlexibleHeader({
    Key key,
    @required this.controller,
    @required this.name,
    @required this.face,
  }) : super(key: key);

  @override
  _HFlexibleHeaderState createState() => _HFlexibleHeaderState();
}

class _HFlexibleHeaderState extends State<HFlexibleHeader> {
  static const double MAX_BOTTOM = 40;
  static const double MIN_BOTTOM = 10;
  // 滚动范围---最大卷曲八十 设置
  static const MAX_OFFSET = 40;
  double _dyBottom = MAX_BOTTOM;

  @override
  void initState() {
    super.initState();
    widget.controller.addListener(() {
      // 滚动距离
      var offset = widget.controller.offset;
      // hyLog(offset, StackTrace.current);
      // 算出padding变化0-1
      var dyOffset = (MAX_BOTTOM - offset) / MAX_OFFSET;
      // 根据 dyOffset算出具体的变化padding值
      var dy = dyOffset * (MAX_BOTTOM - MIN_BOTTOM);
      // 临界值保护
      if (dy > (MAX_BOTTOM - MIN_BOTTOM)) {
        dy = MAX_BOTTOM;
      } else if (dy < 0) {
        dy = 0;
      }
      // 算出实际的padding
      setState(() {
        _dyBottom = MIN_BOTTOM + dy;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.bottomLeft,
      // padding: EdgeInsets.only(bottom: 30, left: 10),
      padding: EdgeInsets.only(bottom: _dyBottom, left: 10),
      child: Row(
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(23),
            child: cachedImage(widget.face, width: 46, height: 46),
          ),
          hiSpace(width: 8),
          Text(
            widget.name,
            style: TextStyle(fontSize: 11),
          )
        ],
      ),
    );
  }
}
